
package monitortest;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JTextField;

/**
 *
 * @author the Dope Factory
 */
public class InterfaceProxy {
    /** JDialog per la scelta della COM e l'avvio del monitoring*/
    JDialog startProx;
    
    /**campo per inserimento indirizzo del server proxy*/
    JTextField address;
    
    /**campo per inserimento porta del server proxy*/
    JTextField port;
    
    /**bottone conferma connessione*/
    JButton start;
    
    /*bottone annulla connessione proxy**/
//    JButton cancel;
    
    Dimension scr = Toolkit.getDefaultToolkit().getScreenSize();
    
    public String addressValue="";
    public int portValue=0;
    
    public InterfaceProxy(){
         //JDialog con scelta porta com
        startProx=new JDialog();
        startProx.setModal(true);
        startProx.setIconImage(Interface.favicon);
        startProx.setBounds((scr.width-300)/2, (scr.height-100)/2, 300, 100);
        startProx.setLayout(null);
        startProx.setTitle("Proxy Connection");
        startProx.setResizable(false);
        JLabel addr=new JLabel("Host IP:");
        JLabel portId=new JLabel("Porta:");
        address=new JTextField();
        port=new JTextField();
        start=new JButton("Start");
        JButton cancel=new JButton("Cancel");
//        startProx.add(intro);
        startProx.add(addr);
        startProx.add(portId);
        startProx.add(address);
        startProx.add(port);
        startProx.add(start);
        startProx.add(cancel);
//        
//        intro.setBounds(5,5,200,25);
        addr.setBounds(50,0,100,20);
        portId.setBounds(175,0,100,20);
        address.setBounds(50,18,100,20);
        port.setBounds(175,18,50,20);
        start.setBounds(50,42,100,25);
        cancel.setBounds(150,42,100,25);
//        
//        startProx.setVisible(true);
        
        start.addActionListener(new ActionListener(){
            @Override
                public void actionPerformed(ActionEvent e){
                    if(address.getText()!=null && port.getText()!=null){
                        int tempPort=Integer.decode(port.getText());
                        
                        addressValue=address.getText();
                        portValue=tempPort;        
                        
                        ProxyRead proxy=new ProxyRead();
                        proxy.connectProxy(address.getText(), tempPort);
                        
                        LedManager led=new LedManager();
                        led.run();
                        
                        ManagementProxy man=new ManagementProxy();
                        man.run();
                    }
                    startProx.dispose();
                }
        });
        
        cancel.addActionListener(new ActionListener(){
            @Override
                public void actionPerformed(ActionEvent e){
                    startProx.dispose();
                }
        });
        startProx.setVisible(true);
    }
    
    public String getAddress(){
        return addressValue;
    }
    
    public int getPort(){
        return portValue;
    }
    
}
